<html>
<head>

<title>Groovy Goodness: Configuring Grape to Use Classloader</title>

<script language="javascript" src="scripts/shCore.js"></script> 
<script language="javascript" src="scripts/shLegacy.js"></script> 
<script language="javascript" src="scripts/shBrushJava.js"></script> 
<script language="javascript" src="scripts/shBrushXml.js"></script> 
<script language="javascript" src="scripts/shBrushJScript.js"></script> 
<script language="javascript" src="scripts/shBrushGroovy.js"></script> 
<script language="javascript" src="scripts/shBrushPlain.js"></script> 
<script language="javascript" src="scripts/shBrushBash.js"></script> 
 
<link href="styles/reset.css" rel="stylesheet" type="text/css" />
<link href="styles/shCore.css" rel="stylesheet" type="text/css" />
<link type="text/css" rel="stylesheet" href="styles/shThemeRDark.css"/>
<link href="styles/blog.css" rel="stylesheet" type="text/css" />

</head>
<body>

<a href="index.html">Back to index</a>

<h3 class="post-title">Groovy Goodness: Configuring Grape to Use Classloader</h3>

<div class="post">
<p>Groovy has Grape to define dependencies which are necessary for our code to be downloaded by Ivy. This way we don't have to distribute third-party libraries with our code, but we only have to define the dependencies in our code. We can define these dependencies with the <code>@Grab</code> annotations. If we want to define multiple <code>@Grab</code> annotation we can group them together within a <code>@Grapes</code> annotation.</p>
<p>By default the dependencies are available in the same classloader as our Groovy script or application. But sometimes this is not enough. For example if we have a script with a dependency on a SQL database driver and in our code <code>java.sql.DriverManager.getConnection()</code> is used we want our driver class to be on the system classloader. That is because the <code>DriverManager</code> class only can 'see' classes in the system classloader and not other classloaders. A possible exception we get is <code>java.sql.SQLException: No suitable driver found</code>.</p>
<p>To support this scenario we add the <code>@GrabConfig</code> annotation and define that the dependency is part of the system classloader. And we are ready to use the dependency in our script.</p>
<pre class="brush:groovy">
@Grapes([
 @Grab('org.slf4j:slf4j-simple:1.5.11'),
 @Grab('mysql:mysql-connector-java:5.1.12'),
 @GrabConfig(systemClassLoader = true)
])
import org.slf4j.*
import groovy.sql.*

def logger = LoggerFactory.getLogger('sql')

logger.info 'Initialize SQL'

def username = args[0]  // First argument passed to script.
def password = args[1]  // Second argument passed to script is password.
def sql = Sql.newInstance('jdbc:mysql://localhost/test', username, password, 'com.mysql.jdbc.Driver')

logger.info "Got myself a SQL connection: $sql"
</pre
</div>

<script language="javascript"> 
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = 'scripts/clipboard.swf';
SyntaxHighlighter.defaults['first-line'] = 0;
SyntaxHighlighter.defaults['auto-links'] = false;
SyntaxHighlighter.all();
dp.SyntaxHighlighter.HighlightAll('code');
</script>

</body>
</html>